//
//  NSAttributedString+Additions.swift
//  MGExtensionsKit
//
//  Created by Max on 2019/8/29.
//

import Foundation

extension NSAttributedString {
    
    public convenience init(string: String, textColor: UIColor? = nil, font: UIFont? = nil, lineSpacing: CGFloat = 0, alignment: NSTextAlignment = .left) {
        
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineBreakMode = .byTruncatingTail
        paragraphStyle.lineSpacing = lineSpacing
        paragraphStyle.alignment = alignment
        paragraphStyle.hyphenationFactor = 1.0
        paragraphStyle.firstLineHeadIndent = 0.0
        paragraphStyle.paragraphSpacingBefore = 0.0
        paragraphStyle.headIndent = 0
        paragraphStyle.tailIndent = 0
        
        var attributes: [NSAttributedString.Key : Any] = [NSAttributedString.Key.paragraphStyle: paragraphStyle]
        if let textColor = textColor {
            attributes[NSAttributedString.Key.foregroundColor] = textColor
        }
        if let font = font {
            attributes[NSAttributedString.Key.font] = font
        }
        
        self.init(string: string, attributes: attributes)
    }
}
